@Typed package org.mbte.groovypp.examples.wordcount

import java.util.concurrent.*
import groovy.util.concurrent.*

for (i in 0..<10) {

    def t1 = System.currentTimeMillis()
    def pool = Executors.newFixedThreadPool(30)
    def counts = new AtomicIntegerMap ()

    new File("./20_newsgroups").recurseFileIterator().filter{ file ->
        !file.directory && !file.path.contains(".svn")
    }.each(pool,4) { file ->
        file.charSequence.eachMatch(/\w+/) { String w -> w = w.toLowerCase()
            counts[w].incrementAndGet ()
        }
    }.whenBound {
        pool.execute {
            new File("counts-descreasing-groovy").withWriter { Writer out ->
              counts.asList().sort { a, b -> b.get() <=> a.get() }.each { e -> out << "$e.key\t${e.get()}\n" }
            }
        } {
            new File("counts-alphabetical-groovy").withWriter { Writer out ->
              counts.asList().sort { a, b -> b.key <=> a.key }.each { e -> out << "$e.key\t${e.get()}\n" }
            }
        }
        pool.shutdown()
    }

    pool.awaitTermination(30,TimeUnit.SECONDS)
    println "Finished in ${System.currentTimeMillis() - t1} millis"
}
